/**
 * jims
 */
package com.jims.master.modules.oper.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.modules.oper.entity.PriceList;
import com.jims.master.modules.oper.service.PriceListService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;


/**
 * 价表Controller
 *
 * @author CTQ
 * @version 2017-02-22
 */
@Controller
@RequestMapping(value = "${adminPath}/oper/priceList")
public class PriceListController extends BaseController {

	@Autowired
	private PriceListService priceListService;
	
	@ModelAttribute
	public PriceList get(@RequestParam(required=false) String id) {
		PriceList entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = priceListService.get(id);
		}
		if (entity == null){
			entity = new PriceList();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(PriceList priceList, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PriceList> page = priceListService.findPage(new Page<PriceList>(request, response), priceList); 
		model.addAttribute("page", page);
		return "modules/oper/priceListIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(PriceList priceList, Model model) {
		priceList.setStartDate(new Date());
		model.addAttribute("priceList", priceList);
		return "modules/oper/priceListForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(PriceList priceList) {
	    StringData data=new StringData();
		int i=priceListService.savePriceList(priceList);
        if(i>0){
            data.setCode("success");
            data.setData("保存价表成功");
        }else{
            data.setCode("error");
            data.setData("保存价表失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(PriceList priceList) {
	    StringData data=new StringData();
		int i=priceListService.delete(priceList);
        if(i>0){
            data.setCode("success");
            data.setData("删除价表成功");
        }else{
            data.setCode("error");
            data.setData("删除价表失败");
        }
		return data;
	}

	/**
	 * @param    priceList         传递参数
	 * @return   String  返回类型
	 * @throws
	 * @Title: updatePriceForm
	 * @Description: (跳转调价页面)
	 * @author CTQ
	 * @date 2017-03-09
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "updatePriceForm")
	public String updatePriceForm(PriceList priceList, Model model) {
		PriceList pl = new PriceList();
		pl.setOrgId(priceList.getOrgId());
		pl.setId(priceList.getId());
		pl.setItemName(priceList.getItemName());
		pl.setStartDate(new Date());
		model.addAttribute("currentPriceList", priceList);
		model.addAttribute("priceList", pl);
		return "modules/oper/priceListUpdatePriceForm";
	}
	/**
	 * @param    priceList         传递参数
	 * @return   StringData  返回类型
	 * @throws
	 * @Title: updatePriceSave
	 * @Description: (保存调价信息)
	 * @author CTQ
	 * @date 2017-03-09
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "updatePriceSave")
	@ResponseBody
	public StringData updatePriceSave(PriceList priceList) {
		StringData data=new StringData();
		int i=priceListService.updatePriceList(priceList);
		if(i>0){
			data.setCode("success");
			data.setData("价表调价成功");
		}else{
			data.setCode("error");
			data.setData("价表调价失败");
		}
		return data;
	}
	/**
	 * @param    priceList         传递参数
	 * @return   StringData  返回类型
	 * @throws
	 * @Title: stopPrice
	 * @Description: (药品停价)
	 * @author CTQ
	 * @date 2017-03-09
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "stopPrice")
	@ResponseBody
	public StringData stopPrice(PriceList priceList) {
		StringData data=new StringData();
		int i=priceListService.stopPriceList(priceList);
		if(i>0){
			data.setCode("success");
			data.setData("价表停价成功");
		}else{
			data.setCode("error");
			data.setData("价表停价失败");
		}
		return data;
	}

	/**
	 * @param    priceList         传递参数
	 * @return   StringData  返回类型
	 * @throws
	 * @Title: stopPrice
	 * @Description: (药品停价后重新启用)
	 * @author CTQ
	 * @date 2017-05-09
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "openPrice")
	@ResponseBody
	public StringData openPrice(PriceList priceList) {
		StringData data=new StringData();
		int i=priceListService.openPriceList(priceList);
		if(i>0){
			data.setCode("success");
			data.setData("价表停价成功");
		}else{
			data.setCode("error");
			data.setData("价表停价失败");
		}
		return data;
	}
	/**
	 * @param    clinicId  ，inputCode，  type     传递参数
	 * @return   List  返回类型
	 * @throws
	 * @Title: autoComplete
	 * @Description: (诊疗项目对照-价表补全)
	 * @author CTQ
	 * @date 2017-03-09
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"autoComplete", ""})
	@ResponseBody
	public List<PriceList> autoComplete(String q,String clinicId,String limit,String type) {
		if(q!=null&&!q.equals("")){
			try {
				q = URLDecoder.decode(q,"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return priceListService.findListByParam(q,clinicId,limit,type);
	}

	/**
	 * 费别查询价表
	 * @param q
	 * @param clinicId
	 * @param limit
	 * @param type
	 * @param chargeType
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"autoCompleteByCharge", ""})
	@ResponseBody
	public List<PriceList> autoCompleteByCharge(String q,String clinicId,String limit,String type,String chargeType) {
		if(q!=null&&!q.equals("")){
			try {
				q = URLDecoder.decode(q,"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return priceListService.findListByCharge(q,clinicId,limit,type,chargeType);
	}
}